package cn.yunhe.servlet;
import cn.yunhe.dao.IUserAccount;
import cn.yunhe.entity.userentity.UserAccount;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * 王羊
 */
@WebServlet("/UserAccountServletYHMDL")
public class UserAccountServletYHMDL extends HttpServlet {
    SqlSession sqlSession = null;
    IUserAccount iuserAccount = null;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        //获取输入的注册名和密码和手机号
        String a = request.getParameter("account");
        String b = request.getParameter("password");
        sqlSession = SqlSessionFactoryUtil.getSqlSession();
        iuserAccount = sqlSession.getMapper(IUserAccount.class);
        Map map = new HashMap();
        map.put("account", a);
        map.put("password", b);
        HttpSession session = request.getSession();
        UserAccount userAccount2 = iuserAccount.denglu2(a);
        //通过用户名查询
        if (userAccount2 == null) {
            out.print("<script>alert('该用户不存在');</script>");
            //输入不正确 跳转至注册页面
            out.print("<script>location.href='login2.html';</script>");

        } else {
            UserAccount userAccount = iuserAccount.denglu(map);
            if (userAccount == null) {
                out.print("<script>alert('输入密码错误');location.href='login2.html';</script>");
            } else {
                session.setAttribute("uid",userAccount.getUid());
                out.print("<script>alert('登陆成功');location.href='index.html';</script>");
            }


        }

    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
